(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.splitAccountController', splitAccountController);
	splitAccountController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'accountService', 'ajaxServiceFactory'];
	function splitAccountController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, accountService, ajaxServiceFactory) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("splitAccountLookup"), data = [];
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getAcctCodes : {
				url : 'do/cda/dataList/accountCode',
				method : 'post'
			}
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.config = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.ok = function() {
			   for (var i = 0; i < ctrl.account.splitAcct.length; i++) {
			    	var splitAccount = ctrl.account.splitAcct[i];
			    	if(!splitAccount.percentage && !splitAccount.acctCode){
				    	modalService.openErrorDialogs({
				    		messages : ['Account Code is required !','Percentage is required !']
						});
				    	return;
				  }else if(!splitAccount.acctCode){
				    	modalService.openErrorDialogs({
							messages : [ 'Account Code is required !' ]
						});
				    	return;
				  }else if(!splitAccount.percentage){
				    	modalService.openErrorDialogs({
							messages : [ 'Percentage is required !' ]
						});
				    	return;
				  }
			    }
			   var split = getSplitAccount(ctrl.account.splitAcct);
			    if(split.length > 1024){
			    	modalService.openErrorDialogs({
						messages : [ 'data is too much !' ]
					});
			    	return;
			    }
			    var percentage = 0;
			    angular.forEach(ctrl.account.splitAcct, function(result, key){
			    	percentage = parseInt(result.percentage) + percentage ;
				});
			    if(percentage > 100 || percentage < 100){
			    	modalService.openErrorDialogs({
						messages : [ 'The total percentage must be equal to 100%!' ]
					});
			    	return;
			    }
			   if(ctrl.account.splitAcct.length>0){
				   for(var n=0; n<ctrl.account.splitAcct.length; n++){
			    		ctrl.account.splitAcct[n].patternflag = ctrl.account.patternflag;
			       }
			   }
			   if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.account) !== false) {
				  $uibModalInstance.close();
			   }
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createInnerGridOptions($scope, ctrl.config.tableConfigs.table, rowSelectionChanged);
			ctrl.gridOptions.data = 'vm.account.splitAcct';
			ctrl.account = {};
			ctrl.account.splitAcct = ctrl.dialogsModule.params.splitAcct || [];
			if(ctrl.account.splitAcct.length > 0){
				if(ctrl.account.splitAcct[0].patternflag){
					ctrl.account.patternflag = ctrl.account.splitAcct[0].patternflag;
				}
			}
			if(ctrl.dialogsModule.params.patternflag){
				ctrl.account.patternflag = ctrl.dialogsModule.params.patternflag;
			}
		    if(ctrl.account.patternflag =="Y"){
				ctrl.gridOptions.columnDefs[1].type = "text";
			}
			if(!ctrl.account.patternflag){
			   ctrl.account.patternflag = "N";
			}
			accountService.getPatternName().then(function(patternNames){
				data = patternNames;
				initAccountCodes();
			});
		};
	
		ctrl.setacct = function (a,b,c){
			if(ctrl.account.patternflag =="Y"){
				ctrl.gridOptions.columnDefs[1].type = "text";
				ctrl.gridOptions.columnDefs[1].patternOptions =  {
						"data": []
				};
			    ctrl.gridOptions.columnDefs[1].patternOptions.data = data;
			    if(c!=true){
				    if(ctrl.account.splitAcct.length>0){
				    	 for(var i = 0; i < ctrl.account.splitAcct.length; i++){
				    		 ctrl.account.splitAcct[i].percentage = null;
				    	 }
				    }
				    if(ctrl.account.splitAcct.length > 0){
				    	for(var n=0; n<ctrl.account.splitAcct.length; n++){
				    		ctrl.account.splitAcct[n].patternflag="Y";
				    	}
				    }
			    }
			}else{
				ctrl.gridOptions.columnDefs[1].type ="percentage";
				if(c!=true){
					if(ctrl.account.splitAcct.length>0){
				    	 for(var i = 0; i < ctrl.account.splitAcct.length; i++){
				    		 ctrl.account.splitAcct[i].percentage = null;
				    	 }
				    }
					if(ctrl.account.splitAcct.length > 0){
				    	for(var n=0; n<ctrl.account.splitAcct.length; n++){
				    		ctrl.account.splitAcct[n].patternflag="N";
				    	}
				    }
				}
			}
		};
		
		function initAccountCodes(){
			var accountCodes = {
					"data" : []
				};
			ajaxService.getAcctCodes().then(function(response) {
				if(response && response.data){
					angular.forEach(response.data, function(data, index, array){
						accountCodes.data.push({"value" : data.value, "label" : data.label});
					});
				}
				ctrl.acctCodes=accountCodes;
				ctrl.acctCodes.hasEmptyOption=true;
				}, angular.noop);
		} 
		
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}
		
		function getSplitAccount(resultList){
			var nameValuePairs = "";
			angular.forEach(resultList, function(result, key){
				if(key != resultList.length - 1) {
					nameValuePairs += result.accountCode + "=" + result.percentage;
					nameValuePairs += ";";
				}else{
					nameValuePairs += result.accountCode + "=" + result.percentage;
				}
			});
			return nameValuePairs;
		}

	}
})(angular);
